-- DeveloperBoard
-- Created by chengb Feb/3/2016
-- 开发组木牌格子

-- 初始化
function initDeveloperBoard(self)
    self.item:removeAllChildren();
    self.item:setVisible(true);

    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    if not buildingInfo then
        error("获取不到建筑查看配置，元素类型：" .. class);
        return;
    end

    local id = self.gridData.id;
    local element = self.gridData.element;
    local pos = self.gridData:getPos();
    local function callFunc()
        if id == 1 then
            -- 第一次阅读

            -- 标记正在转换中
            self.transforming = true;

            -- 等界面关闭片刻后播放动画
            local function refresh()
                -- 播放“滴滴”音效
                AudioM.playFx("button_return");

                alert(getLocStr("board_refreshing"));

                local function transform()
                    -- 转换木牌
                    DungeonActionM.go("element_transform", pos);
                end
                -- 播放木牌刷新动画
                playBoardTransformEffect(self, transform);
            end
            -- 播放效果，锁屏
            wait(2.0);
            performWithDelay(self, refresh, 0.5);
        elseif id == 2 then
            -- 第二次阅读

            -- 标记正在转换中
            self.transforming = true;

            local altarEffectParent = findChildByName(self.node, "effect_cut_node");
            local dustEffectParent = findChildByName(self.node, "effect_node");
            altarEffectParent:setVisible(true);
            dustEffectParent:setVisible(true);

            -- 刷成祭坛
            local function appearOddAltar()
                DungeonActionM.go("element_transform", pos);
                
                local function delay()
                    altarEffectParent:setVisible(false);
                    dustEffectParent:setVisible(false);
                end
                performWithDelay(self, delay, 1.0);
            end

            local function doAppearOddAltar()
                -- 去掉木牌
                self.item:removeAllChildren();
                -- 播放奇怪的祭坛出现动画
                playOddAltarAppearEffect(self, appearOddAltar, altarEffectParent, dustEffectParent);
                -- 测试用重复播放
                -- performWithDelay(self, doAppearOddAltar, 10);
            end
            -- 播放效果，锁屏
            wait(5.0);
            performWithDelay(self, doAppearOddAltar, 0.3);
        else
            -- 其他情况，暂不处理
        end
    end

    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                return;
            end

            if self.transforming then
                cclog("开发组木牌格子正在转换中。");
                return;
            end

            -- 音效
            AudioM.playFx("ui_open1");

            -- 打开建筑查看界面
            -- 需要隐藏返回按钮，强制要求玩家点击确定
            if type(id) == "number" then
                -- 如果配置了id，则打开带额外描述的界面
                self:openBuildingView3(callFunc, nil, nil, nil, nil, nil, nil, true);
            else
                self:openBuildingView(callFunc);
            end
        end
    end

    -- 建筑图标
    local iconPath = getLevelItemIconPath(buildingInfo["icon"]);

    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.icon:addTouchEventListener(onClicked);
    self.item:addChild(self.icon);
    self.iconPath = iconPath;
end
